package com.ymatou.iapi.godproductsearch.testcase;

import java.util.Map;

import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.shop2cn.iapi.agentproducts.parameter.AddAgentProductBean;
import com.shop2cn.iapi.agentproducts.service.AddAgentProductCall;
import com.shop2cn.iapi.agentproducts.service.ImportAgentProductCall;
import com.ymatou.iapi.dguser.parameter.UserCreateBean;
import com.ymatou.iapi.godmch.parameter.createmchBean;
import com.ymatou.iapi.godmch.parameter.editmchBean;
import com.ymatou.iapi.godproductsearch.parameter.getProductThirdCategoryBean;
import com.ymatou.iapi.godproductsearch.service.getProductThirdCategoryCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.AddAgentProductCallService;
import com.ymttest.business.service.DGUserCallService;
import com.ymttest.business.service.shenqi.GodmchManageCallService;
import com.ymttest.database.model.YmtProductsI;
import com.ymttest.database.sqlwapper.YmtProductCategoryWapper;
import com.ymttest.database.sqlwapper.YmtProductsIWapper;
import com.ymttest.utils.TestDataManager;
/**
*获取商家二级分类
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_getProductThirdCategory{
	private static getProductThirdCategoryBean bean;
	private static getProductThirdCategoryCall call;
	private  AddAgentProductCall addagentproductCall=new AddAgentProductCall();
	private ImportAgentProductCall importagentproductCall=new ImportAgentProductCall();
	private YmtProductsIWapper YmtProductsIWapper =new YmtProductsIWapper();
	private YmtProductCategoryWapper ymtProductCategoryWapper=new YmtProductCategoryWapper();
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("获取商家二级分类");
	}

	@Before
	public void caseUp() {
		bean=new getProductThirdCategoryBean();
		call=new getProductThirdCategoryCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(call.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getProductThirdCategory_001() {
		Logger.start(true,"正例-获取商家二级分类");
		try {

			//创建商户 渠道
			UserCreateBean usercreateBean1=DGUserCallService.initUsercreateBean();
			int userId_channl=DGUserCallService.UserCreateCall(usercreateBean1);
			
			createmchBean createmchbean=GodmchManageCallService.InitcreatemchBeanForChannel(userId_channl);
			GodmchManageCallService.createmchCallForChannel(createmchbean);
			editmchBean editmchbean = GodmchManageCallService.IniteditmachBean(userId_channl);
			GodmchManageCallService.editmachCall(editmchbean);
			//创建商品1
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(userId_channl,usercreateBean1.getNickname(),false);
			addagentproductCall.setData(addAgentProductBean);
			addagentproductCall.callService();
			String product1 = addagentproductCall.getJsonObject().get("data").getAsJsonObject().get("newProductId").getAsString();
			Thread.sleep(2000);
			//商品2
			addagentproductCall.setData(addAgentProductBean);
			addagentproductCall.callService();
			String product2 = addagentproductCall.getJsonObject().get("data").getAsJsonObject().get("newProductId").getAsString();
			Logger.comment("###  product1  ###  "+product1);
			Logger.comment("###  product2  ###  "+product2);
			Thread.sleep(5000);
			Logger.comment("#### 查询一级分类  ###");
			//找一级分类
			YmtProductsI YmtProductsI=YmtProductsIWapper.selectProductByProductId(product1).get(0);
			//二级
			Map category2= ymtProductCategoryWapper.getProductCategoryByCategoryId(YmtProductsI.getiCategoryId()).get(0);
			//es查询
            bean.setSellerId(userId_channl);
            bean.setPageIndex(1);
            bean.setPageSize(2);
            bean.setappId("es搜索_"+TestDataManager.getRandomIntNum(10));
			call.setData(bean);
			call.callService();			
			//校验
			//返回的三级分类
			Integer categoryId3=Integer.parseInt(new JSONObject(call.getCategoryDtos().get(0).toString()).get("categoryId").toString());
			//数据库获取的二级分类
			String DBcategoryId2=ymtProductCategoryWapper.getProductCategoryByCategoryId(categoryId3).get(0).get("iMasterCategory").toString();
			Logger.verifyEquals("true", call.getSuccess(), "验证返回的success");
			//相同的二级分类下有多个三个分离，所以先把返回的三级分类去查询二级分类，然后再与数据库中的二级分类 进行验证
			Logger.verifyEquals(DBcategoryId2, new JSONObject(call.getCategoryDtos().get(0).toString()).get("parentCategoryId").toString(), "验证一级分类Id");
			//Logger.verifyEquals(category2.get("iCategoryId").toString(), new JSONObject(call.getCategoryDtos().get(0).toString()).get("categoryId").toString(), "验证二级分类Id");	
			//Logger.verifyEquals(category2.get("sCategory").toString(),new JSONObject(call.getCategoryDtos().get(0).toString()).get("categoryName").toString(), "验证二级分类Name");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getProductThirdCategory_002() {
		Logger.start(true,"反例-获取商家二级分类-sellerId不存在");
		try {

			//创建商户 渠道
			UserCreateBean usercreateBean1=DGUserCallService.initUsercreateBean();
			int userId_channl=DGUserCallService.UserCreateCall(usercreateBean1);
			
			createmchBean createmchbean=GodmchManageCallService.InitcreatemchBeanForChannel(userId_channl);
			GodmchManageCallService.createmchCallForChannel(createmchbean);
			editmchBean editmchbean = GodmchManageCallService.IniteditmachBean(userId_channl);
			GodmchManageCallService.editmachCall(editmchbean);
			//创建商品1
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(userId_channl,usercreateBean1.getNickname(),false);
			addagentproductCall.setData(addAgentProductBean);
			addagentproductCall.callService();
			String product1 = addagentproductCall.getJsonObject().get("data").getAsJsonObject().get("newProductId").getAsString();
			Thread.sleep(2000);
			//商品2
			addagentproductCall.setData(addAgentProductBean);
			addagentproductCall.callService();
			String product2 = addagentproductCall.getJsonObject().get("data").getAsJsonObject().get("newProductId").getAsString();
			Logger.comment("###  product1  ###  "+product1);
			Logger.comment("###  product2  ###  "+product2);
			Thread.sleep(5000);
			Logger.comment("#### 查询一级分类  ###");
			//找一级分类
			YmtProductsI YmtProductsI=YmtProductsIWapper.selectProductByProductId(product1).get(0);
			//二级
			Map category2= ymtProductCategoryWapper.getProductCategoryByCategoryId(YmtProductsI.getiCategoryId()).get(0);
			//es查询

            bean.setSellerId(userId_channl+1);
            bean.setPageIndex(1);
            bean.setPageSize(2);
            bean.setappId("es搜索_"+TestDataManager.getRandomIntNum(10));
			call.setData(bean);
			call.callService();			
			//校验
			Logger.verifyEquals("true", call.getSuccess(), "验证返回的success");
			Logger.verifyEquals(0, call.getCategoryDtos().length(), "验证categoryDtos长度");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}